package com.xpoint.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xpoint.backend.common.R;
import com.xpoint.backend.dto.SetmealDto;
import com.xpoint.backend.entity.Setmeal;
import com.xpoint.backend.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    @Cacheable(value = "SetmealDtoPage",key = "#page+'_page_'+#pageSize")
//    分页查询
    public R<Page> findPage(Long page, Long pageSize, String name){
        return setmealService.findPage(page,pageSize,name);
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value = {"SetmealDto","SetmealDtoPage"},allEntries = true)
//    批量启售或停售
    public R<String> updateBatchStatusOrStatus(@PathVariable int status,@RequestParam("ids") List<Long> id){
        return setmealService.updateBatchStatus(status,id);
    }

    @DeleteMapping
    @CacheEvict(value = {"SetmealDto","SetmealDtoPage"},allEntries = true)
//    批量删除
    public R<String> deleteBatchById(@RequestParam("ids") List<Long> id){
        return setmealService.delelteSetmealBatchById(id);
    }

    @PostMapping
    @CacheEvict(value = {"SetmealDto","SetmealDtoPage"},allEntries = true)
//    添加套餐
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto){
        return setmealService.addSetmeal(setmealDto);
    }

    @GetMapping("/{id}")
//    根据id查询套餐
    public R<SetmealDto> findSetmeal(@PathVariable Long id){
        return setmealService.findSetmeaById(id);
    }

    @PutMapping
//    根据id修改套餐
    @CacheEvict(value = {"SetmealDto","SetmealDtoPage"},allEntries = true)
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        return setmealService.UpdateSetmeal(setmealDto);
    }

    @GetMapping("/list")
//    根据分类查询所有套餐
    @Cacheable(value = "SetmealDto",key = "#status+'_'+#id")
    public R<List<SetmealDto>> findList(@RequestParam("categoryId") String id, @RequestParam("status")Integer status){
        return setmealService.findList(id,status);
    }
}
